package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.ArticleModuleMapper;
import com.winshare.saleAssistant.domain.dto.req.*;
import com.winshare.saleAssistant.domain.entity.ArticleModuleDO;
import com.winshare.saleAssistant.domain.query.ArticleModuleQuery;
import com.winshare.saleAssistant.domain.dto.resp.ArticleModuleRespDTO;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.mapper.ArticleModuleMapStruct;
import com.winshare.saleAssistant.service.ArticleModuleService;
import com.winshare.saleAssistant.util.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleModuleServiceImpl implements ArticleModuleService {

    private final ArticleModuleMapper articleModuleMapper;

    public ArticleModuleServiceImpl(ArticleModuleMapper articleModuleMapper) {
        this.articleModuleMapper = articleModuleMapper;
    }

    @Override
    public void save(ArticleModuleCreateDTO articleModuleCreateDTO) {
        ArticleModuleDO articleModuleDO = ArticleModuleMapStruct.INSTANCE
                .fromArticleModuleCreateDTO(articleModuleCreateDTO);
        if(checkArticleModuleExist(articleModuleDO)) {
            throw new BusinessException("该栏目在系统中已经存在");
        }
        articleModuleMapper.saveArticleModule(articleModuleDO);
    }

    @Override
    public void update(ArticleModuleUpdateDTO articleModuleUpdateDTO) {
        ArticleModuleDO articleModuleDO = ArticleModuleMapStruct.INSTANCE
                .fromArticleModuleUpdateDTO(articleModuleUpdateDTO);
        if(checkArticleModuleExist(articleModuleDO)) {
            throw new BusinessException("该栏目在系统中已经存在");
        }
        articleModuleMapper.updateArticleModule(articleModuleDO);
    }

    @Override
    public void delete(ArticleModuleDeleteDTO articleModuleDeleteDTO) {
        ArticleModuleDO articleModuleDO = ArticleModuleMapStruct.INSTANCE
                .fromArticleModuleDeleteDTO(articleModuleDeleteDTO);
        articleModuleMapper.deleteArticleModule(articleModuleDO);
    }

    @Override
    public Page<ArticleModuleRespDTO> getArticleModuleList(ArticleModuleQueryDTO articleModuleQueryDTO) {
        ArticleModuleQuery articleModuleQuery = ArticleModuleMapStruct.INSTANCE
                .fromArticleModuleQueryDTO(articleModuleQueryDTO);
        articleModuleQuery.setOffsetIndex(articleModuleQueryDTO.getOffsetIndex());
        int totalCount = articleModuleMapper.queryArticleModuleCount(articleModuleQuery);
        Integer pageIndex = articleModuleQueryDTO.getPageIndex();
        Integer pageSize = articleModuleQueryDTO.getPageSize();
        if(0 == totalCount) {
            return new Page<>(pageIndex,pageSize,totalCount,new ArrayList<>());
        }
        List<ArticleModuleDO> articleModuleDOList = articleModuleMapper
                .findArticleModuleByConditions(articleModuleQuery);
        List<ArticleModuleRespDTO> articleModuleRespDTOList = ArticleModuleMapStruct.INSTANCE
                .fromArticleModuleRespDTOList(articleModuleDOList);
        return new Page<>(pageIndex,pageSize,totalCount,articleModuleRespDTOList);
    }

    @Override
    public ArticleModuleRespDTO getArticleModuleDetail(BaseIdDTO baseIdDTO) {
        ArticleModuleDO articleModuleDO = ArticleModuleMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        ArticleModuleDO articleModuleDOInSql = articleModuleMapper.getArticleModule(articleModuleDO);
        if(null == articleModuleDOInSql) {
            throw new BusinessException("当前栏目不存在");
        }
        ArticleModuleRespDTO articleModuleRespDTO = ArticleModuleMapStruct
                .INSTANCE.fromArticleModuleDO(articleModuleDOInSql);
        return articleModuleRespDTO;
    }

    private boolean checkArticleModuleExist(ArticleModuleDO articleModuleDO) {
        return articleModuleMapper.countByArticleModuleName(articleModuleDO) > 0;
    }
}
